import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        hasLogin: false,
        userInfo: {},
        routerInfo: {},
        leftMensHide: false
    },
    mutations: {
        /**
         * 登录
         * @param {Object} state
         * @param {Object} provider
         */
        login(state, provider) {
            state.hasLogin = true;
            state.userInfo = provider;
            uni.setStorage({
                //缓存用户登陆状态
                key: 'userInfo',
                data: provider
            })
            uni.removeStorage({
                key: "leftMensHide"
            })
        },
        /** 登出
         * @param {Object} state
         */
        logout(state) {
            state.hasLogin = false;
            state.userInfo = {};
            uni.clearStorage()
        },
        /* 刷新状态 */
        refresh(state) {
            let provider = uni.getStorageSync("userInfo") || null;
            if (provider) {
                state.hasLogin = true;
                state.userInfo = provider;
            } else {
                state.hasLogin = false;
            }
            //当前路由信息
            let routerInfo = uni.getStorageSync("currentRouter") || null;
            state.routerInfo = routerInfo;

            //左侧菜单显示隐藏状态
            let hide = uni.getStorageSync("leftMensHide") || false;
            state.leftMensHide = hide;
        },
        /**
         * 记录当前路由信息
         */
        router(state, routerInfo) {
            state.routerInfo = routerInfo;
            uni.setStorage({
                key: 'currentRouter',
                data: routerInfo
            })
        },
        /* 左侧菜单是否隐藏 */
        isLeftMensHide(state, hide) {
            state.leftMensHide = hide;
            uni.setStorage({
                key: 'leftMensHide',
                data: hide
            })
        }
    }
})
export default store
